package dj.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dj.dao.BaseDao;
import dj.model.OperateLog;
import dj.pageModel.Puser;
import dj.service.OperateLogService;
import dj.utils.DateUtils;

@Service("opserateLogService")
public class OperateLogServiceImpl implements OperateLogService {

	private BaseDao<OperateLog> operateLogDao;

	public BaseDao<OperateLog> getOperateLogDao() {
		return operateLogDao;
	}

	@Autowired
	public void setOperateLogDao(BaseDao<OperateLog> operateLogDao) {
		this.operateLogDao = operateLogDao;
	}

	public List<OperateLog> getOperateLogPagination() {
		String p = ServletActionContext.getRequest().getParameter("page");
		String r = ServletActionContext.getRequest().getParameter("rows");
		int page = 0;
		int rows = 0;
		try {
			page = Integer.parseInt(p);
			rows = Integer.parseInt(r);
		} catch (NumberFormatException e1) {
			e1.printStackTrace();
		}
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");

		String hql = "from OperateLog where 1=1 ";
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != startDate && !"".equals(startDate)) {
			hql += "and operateTime>:startDate ";
			params.put("startDate", DateUtils.toDate(startDate, DateUtils.DEFAULT_DATE_PATTERN));
		}
		if (null != endDate && !"".equals(endDate)) {
			hql += "and operateTime<:endDate ";
			params.put("endDate", DateUtils.toDate(endDate, DateUtils.DEFAULT_DATE_PATTERN));
		}
		hql += "order by operateTime desc";

		return operateLogDao.find(hql, params, page, rows);
	}

	public Long getOperateLogNum() {
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");

		String hql = "select count(o) from OperateLog o where 1=1 ";
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != startDate && !"".equals(startDate)) {
			hql += "and operateTime>:startDate ";
			params.put("startDate", DateUtils.toDate(startDate, DateUtils.DEFAULT_DATE_PATTERN));
		}
		if (null != endDate && !"".equals(endDate)) {
			hql += "and operateTime<:endDate ";
			params.put("endDate", DateUtils.toDate(endDate, DateUtils.DEFAULT_DATE_PATTERN));
		}
		return operateLogDao.getCount(hql, params);
	}

	public List<OperateLog> getCompanyOperateLogPagination(Puser loginUser) {
		String p = ServletActionContext.getRequest().getParameter("page");
		String r = ServletActionContext.getRequest().getParameter("rows");
		int page = 0;
		int rows = 0;
		try {
			page = Integer.parseInt(p);
			rows = Integer.parseInt(r);
		} catch (NumberFormatException e1) {
			e1.printStackTrace();
		}

		String hql = "select o from OperateLog o, Tuser u where o.user = u.name and u.parentid=:parentid";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("parentid", loginUser.getId());

		return operateLogDao.find(hql, params, page, rows);
	}

	public Long getCompanyOperateLogNum(Puser loginUser) {
		String hql = "select count(o) from OperateLog o, Tuser u where o.user = u.name and u.parentid=:parentid";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("parentid", loginUser.getId());

		return operateLogDao.getCount(hql, params);
	}

	public OperateLog getOperateById(String id) {
		String hql = "from OperateLog where id=:id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		
		return operateLogDao.get(hql, params);
	}

}
